<?php

/**
 *   OPENSHOP_taobao 商品相关设置
 * 
 *   @link        http://baison.com.cn
 *   @copyright   Baison, Inc.
 *   @package     OpenShop
 *   @version     $Id: api_tb_app.php,v 1.0 2009/11/5 07:35:58 add $
 *   @author      winner <jsycywm@hotmail.com>
 */

define('IN_OS', true);

define('ROOT_PATH2', str_replace('includes/modules/taobao.api/top_return_call.php', '', str_replace('\\', '/', __FILE__)));

require_once(ROOT_PATH2.'includes/init.php'); 

/* act操作项的初始化 */
/**
 * http://joeone.taobao.com/?
 * top_appkey=12017877&
 * top_parameters=aWZyYW1lPTEmdHM9MTI2MDI0NDI1NTA5MyZ2aWV3X21vZGU9ZnVsbCZ2aWV3X3dpZHRoPTAmdmlzaXRvcl9pZD0yOTMwMTU3NzMmdmlzaXRvcl9uaWNrPb7FxMHN9bnZt73G7L2i&
 * top_session=179459dc1f1d8c00a9d67b598e9a845dd&
 * top_sign=JCFrlQsfYTOAhvK7J3TaFQ%3D%3D&agreement=true&
 * agreementsign=12017877-21004070-3041CEA215774123EA71DC4171EA004C&y=3&x=40
*/

//	获取淘宝传递过来的参数
	$top_appkey = $_REQUEST['top_appkey'];
	$top_session = $_REQUEST['top_session'];
	
//	更新应用值,并设为当前应用
	$sql ="update open_tb_app set app_session= '".$top_session."' ,is_use = 1 where app_key='".$top_appkey."'"; 
	$db->query($sql);
	
	$sql ="update open_tb_app set is_use = 0 where app_key!='".$top_appkey."'"; 
	$db->query($sql);
	
//	更新shopconfig数据
	$sql = "select * from  open_tb_app where app_key = '".$top_appkey."' "; 
	$app = $db->getRow($sql);
	
	$sql = "update open_shop_config set value = '".$app['app_key']."' where code='taobao_app_key'";
	$db->query($sql);
	$sql = "update open_shop_config set value = '".$app['app_secret']."' where code='taobao_app_secret'";
	$db->query($sql);
	$sql = "update open_shop_config set value = '".$app['app_session']."' where code='taobao_sessionkey'";
	$db->query($sql);
	$sql = "update open_shop_config set value = '".$app['nick']."' where code='taobao_user'";
	$db->query($sql);
	
	
	
	$sql="update open_shop_config,open_tb_app set value=nick   where code='taobao_user' and app_key=".$app['app_key'] ;
	$db->query($sql); 
	$sql="  update open_shop_config,open_tb_app set value=app_secret   where code='taobao_app_secret' and app_key=".$app['app_key'] ;
	$db->query($sql); 
	$sql="  update open_shop_config,open_tb_app set value=app_key   where code='taobao_app_key' and app_key=".$app['app_key'] ;
	$db->query($sql); 
	$sql="  update open_shop_config,open_tb_app set value =ifnull(app_session,'')   where code='taobao_sessionkey' and app_key=".$app['app_key'] ;
	$db->query($sql); 
	$sql="  update open_tb_app set is_use=0  ";
	$db->query($sql); 
	$sql="  update open_tb_app set is_use=1  where app_key=".$app['app_key'] ;
	$db->query($sql); 
	clear_cache_files();  /* 清除缓存 */ 
		
	//更新缓存
//	$url = 'http://'.$_SERVER ['HTTP_HOST'].$_SERVER['PHP_SELF'];
//	echo str_replace('top_return_call.php', '', $url);
//	exit;
//	Header("Location:".$url);
	
	echo "<html><body>";
	echo "<p style='text-align:center; padding-top:50px;'>";
	echo "恭喜你，SessionKey已经更新成功，再次反还到原来页面！<br><br>";
	echo "<input type=button value='关闭窗口' onclick='javascript:window.close();'>";
	echo "</p>";
	echo "</body></html>";
	
?>